﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.OCRMax;
using Cognex.VisionPro.OCVMax;
using Cognex.VisionPro.PixelMap;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Blob;
using Application = System.Windows.Forms.Application;
using Cognex.VisionPro.ID;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Implementation;
using Cognex.VisionPro.ToolBlock;




namespace Delta
{
    public partial class AdvancedVisionForm : Form
    {
        public VisionSetForm visionsetform;

        public CogToolBlock cogToolBlock = new CogToolBlock();

        string ToolType = "";


        public string ToolName;

        CogFindCircleEditV2 cogFindCircleEditV2=null;

        CogFindLineEditV2 cogFindLineEditV2 = null;

        CogCaliperEditV2 cogCaliperEditV2 = null;

        CogBlobEditV2 cogBlobEditV2 = null;

        
           

        public AdvancedVisionForm(VisionSetForm form,string toolType)
        {
            InitializeComponent();
            visionsetform = form;
            ToolType = toolType;

            

        }

        private void bSave_Click(object sender, EventArgs e)
        {
            Cog.myCogToolBlock = cogToolBlock;
            Cog.SaveVpp(Vpptype.myCogToolBlock1, "存储地址");
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            Cog.SaveVpp(Vpptype.myCogToolBlock1, "存储地址");
        }



        private void AdvancedVisionForm_Load(object sender, EventArgs e)
        {
            CogToolCollection Tools;
            switch (ToolType)
            {
                case "Circle":



                    cogToolBlock = Cog.myCogToolBlock;
                    Tools = cogToolBlock.Tools;

                    VisionToolList.Items.Clear();
                    for (int i = 0; i < Tools.Count; i++)
                    {
                        if (Tools[i] is CogFindCircleTool)
                        {
                            VisionToolList.Items.Add(Tools[i].Name);
                        }
                    }

                    cogFindCircleEditV2 = new CogFindCircleEditV2();
                    cogFindCircleEditV2.Dock = System.Windows.Forms.DockStyle.Fill;
                    cogFindCircleEditV2.SuspendElectricRuns = false;
                    panel1.Controls.Add(cogFindCircleEditV2);

                    break;
                case "Line":
                    cogToolBlock = Cog.myCogToolBlock;
                    Tools = cogToolBlock.Tools;

                    VisionToolList.Items.Clear();
                    for (int i = 0; i < Tools.Count; i++)
                    {
                        if (Tools[i] is CogFindLineTool)
                        {
                            VisionToolList.Items.Add(Tools[i].Name);
                        }
                    }

                    cogFindLineEditV2 = new CogFindLineEditV2();
                    cogFindLineEditV2.Dock = System.Windows.Forms.DockStyle.Fill;
                    cogFindLineEditV2.SuspendElectricRuns = false;
                    panel1.Controls.Add(cogFindLineEditV2);


                    break;
                case "Caliper":
                    cogToolBlock = Cog.myCogToolBlock;
                    Tools = cogToolBlock.Tools;

                    VisionToolList.Items.Clear();
                    for (int i = 0; i < Tools.Count; i++)
                    {
                        if (Tools[i] is CogCaliperTool)
                        {
                            VisionToolList.Items.Add(Tools[i].Name);
                        }
                    }

                    cogCaliperEditV2 = new CogCaliperEditV2();
                    cogCaliperEditV2.Dock = System.Windows.Forms.DockStyle.Fill;
                    cogCaliperEditV2.SuspendElectricRuns = false;
                    panel1.Controls.Add(cogCaliperEditV2);

                    break;
                case "blob":
                    cogToolBlock = Cog.myCogToolBlock;
                    Tools = cogToolBlock.Tools;

                    VisionToolList.Items.Clear();
                    for (int i = 0; i < Tools.Count; i++)
                    {
                        if (Tools[i] is CogBlobTool)
                        {
                            VisionToolList.Items.Add(Tools[i].Name);
                        }
                    }

                    cogBlobEditV2 = new CogBlobEditV2();
                    cogBlobEditV2.Dock = System.Windows.Forms.DockStyle.Fill;
                    cogBlobEditV2.SuspendElectricRuns = false;
                    panel1.Controls.Add(cogBlobEditV2);

                    break;
            }
            if (VisionToolList.Items.Count > 0)
            {
                VisionToolList.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("检测流程中未包含"+ "ToolType"+ "工具");
            }
        }

        private void VisionToolList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ToolName = VisionToolList.Text;

            switch (ToolType)
            {
                case "Circle":
                    if(cogFindCircleEditV2!=null)
                    {
                        cogFindCircleEditV2.Subject = cogToolBlock.Tools[ToolName] as CogFindCircleTool;
                    }
                    
                    break;
                case "Line":
                    if (cogFindLineEditV2 != null)
                    {
                        cogFindLineEditV2.Subject = cogToolBlock.Tools[ToolName] as CogFindLineTool;
                    }
                    break;
                case "Caliper":
                    if (cogCaliperEditV2 != null)
                    {
                        cogCaliperEditV2.Subject = cogToolBlock.Tools[ToolName] as CogCaliperTool;
                    }
                    break;
                case "blob":
                    if (cogBlobEditV2 != null)
                    {
                        cogBlobEditV2.Subject = cogToolBlock.Tools[ToolName] as CogBlobTool;
                    }
                    break;
            }



        }
    }
}
